class TextBlock(object):
    __slots__ = ["lines", "dedent"]
    def __init__(self, *lines, **kwargs):
        self.dedent = kwargs.pop("dedent", 0)
        if kwargs:
            raise TypeError("got unexpected keyword arguments: %s" % (kwargs.keys(),))
        self.lines = list(lines)
    def append(self, *lines):
        self.lines.extend(lines)
    def create_child(self, *lines, **kwargs):
        b = TextBlock(*lines, **kwargs)
        self.append(b)
        return b
    def skip(self, num_of_lines = 1):
        self.lines.append("" * num_of_lines)
    def __str__(self):
        return self.render()
    def render(self, nesting = 0, tab = "    "):
        nesting -= self.dedent
        ind = nesting * tab
        text = []
        for line in self.lines:
            if isinstance(line, TextBlock):
                text.append(line.render(nesting + 1, tab))
            elif line is None:
                pass
            elif line == "":
                text.append(ind)
                text.append("\n")
            else:
                for l in str(line).splitlines():
                    text.append(ind)
                    text.append(l)
                    text.append("\n")
        return "".join(text)



if __name__ == "__main__":
    doc = TextBlock(
        "#include <moshe.h>",
        "#include <baruch.h>",
        "",
        "int main(int argc, char ** argc)",
        "{",
        TextBlock(
            "if (argc <= 1)",
            "{",
            TextBlock('printf("usage: %s ", argc[0]);'),
            "}",
            "",
            "return 17;"
        ),
        "}",
    )
    print doc.render(tab = "  ")













